package main

import (
	"fmt"
	"go-base-learning/src/16_project02_customer/customerManager/service"
)

// customerView 显示
type customerView struct {
	// key 选择
	key string
	// loop 是否循环
	loop            bool
	customerService *service.CustomerService
}

func (cv *customerView) mainView() {
	for {
		fmt.Println("--------------------客户信息管理软件--------------------")
		fmt.Println("                    1、添加客户")
		fmt.Println("                    2、修改客户")
		fmt.Println("                    3、删除客户")
		fmt.Println("                    4、客户列表")
		fmt.Println("                    5、退    出")
		fmt.Print("                    请选择(1-5)：")
		fmt.Scanf("%s", &cv.key)
		switch cv.key {
		case "1":
			cv.add()
		case "2":
			cv.modify()
		case "3":
			cv.del()
		case "4":
			cv.list()
		case "5":
			cv.exit()
		default:
			fmt.Printf("你的输入有误，请重新输入: [%v]\n", cv.key)
		}
		if !cv.loop {
			break
		}
	}
	fmt.Println("已退出客户信息管理系统.....")
}

func (cv *customerView) add() {
	var name, gender, tel, email string
	var age int
	fmt.Println("--------------------添加客户--------------------")
	fmt.Print("姓名：")
	fmt.Scanf("%s", &name)
	fmt.Print("性别：")
	fmt.Scanf("%s", &gender)
	fmt.Print("年龄：")
	fmt.Scanf("%d", &age)
	fmt.Print("电话：")
	fmt.Scanf("%s", &tel)
	fmt.Print("邮箱：")
	fmt.Scanf("%s", &email)
	success, err := cv.customerService.Add(name, gender, age, tel, email)
	if success {
		fmt.Println("--------------------添加成功--------------------")
	} else {
		fmt.Println(err)
		fmt.Println("--------------------添加失败--------------------")
	}
}

func (cv *customerView) modify() {
	var id int = -1
	fmt.Println("--------------------修改客户--------------------")
	fmt.Println("请选择待修改客户编号(-1退出)：")
	fmt.Scanf("%d", &id)
	if id == -1 {
		return
	}
	success, customer := cv.customerService.GetById(id)
	if success {
		var name string = customer.Name
		var gender string = customer.Gender
		var tel string = customer.Tel
		var email string = customer.Email
		var age int = customer.Age
		fmt.Println("--------------------添加客户--------------------")
		fmt.Printf("姓名(%s)：\n", name)
		fmt.Scanf("%s", &name)
		fmt.Printf("性别(%s)：\n", gender)
		fmt.Scanf("%s", &gender)
		fmt.Printf("年龄(%d)：\n", age)
		fmt.Scanf("%d", &age)
		fmt.Printf("电话(%s)：\n", tel)
		fmt.Scanf("%s", &tel)
		fmt.Printf("邮箱(%s)：\n", email)
		fmt.Scanf("%s", &email)
		success, err := cv.customerService.Modify(id, name, gender, age, tel, email)
		if success {
			fmt.Println("--------------------修改成功--------------------")
		} else {
			fmt.Printf("--------------------修改失败, %s--------------------\n", err)
		}
	} else {
		fmt.Printf("--------------------修改失败, id不存在--------------------\n")
	}
}

func (cv *customerView) del() {
	var id int = -1
	var choice string
	fmt.Println("--------------------删除客户--------------------")
	fmt.Println("请选择待删除客户编号(-1退出)：")
	fmt.Scanf("%d", &id)
	if id == -1 {
		return
	}
	for {
		fmt.Println("请确认是否删除(y/n)：")
		fmt.Scanf("%s", &choice)
		if choice == "y" || choice == "Y" {
			success, err := cv.customerService.Del(id)
			if success {
				fmt.Println("--------------------删除成功--------------------")
			} else {
				fmt.Printf("--------------------删除失败, %s--------------------\n", err)
			}
			break
		} else if choice == "n" || choice == "N" {
			break
		}
	}
}

func (cv *customerView) list() {
	fmt.Println("--------------------客户列表--------------------")
	customers := cv.customerService.List()
	fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
	for _, customer := range customers {
		fmt.Println(customer)
	}
	fmt.Println("--------------------结    束--------------------")
}

func (cv *customerView) exit() {
	var key string
	for {
		fmt.Println("请确认是否退出(y/n)：")
		fmt.Scanf("%s", &key)
		if key == "y" || key == "Y" {
			cv.loop = false
			break
		} else if key == "n" || key == "N" {
			cv.loop = true
			break
		}
	}
}

func main() {
	var customerManager = customerView{
		key:             "",
		loop:            true,
		customerService: service.NewCustomerService(),
	}
	customerManager.mainView()
}
